module ArticlesHelper
  def show_code(content, syntax, theme, line_div = "")
    format_code_with_no_line_numbers = code(content, :syntax => syntax, :theme => theme)
    format_code_with_line_numbers = code(content, :syntax => syntax, :line_numbers => false, :theme => theme)
    # Just reserve line numbers.
    # example: <span class="line-numbers">   1 </span>
    regex = /<[\w]* class="line-numbers">\s*[0-9]*\s<\/span>/
    format_code_with_no_line_numbers.scan(regex).each { |m| line_div << m }
    line_div = content_tag(:div, line_div, :class => "lines_warp")
    # Source with out line number, because we use HAML so should be some simple format.
    code_div = content_tag(:div, content_tag(:div, format_code_with_line_numbers), :class => "source_warp")
    code_div.gsub!(/(\n)/, "<br />")
    
    content_tag(:table, 
      content_tag(:tbody, 
        content_tag(:tr, 
          content_tag(:td, line_div) + content_tag(:td, code_div)
        )
      ), :cellspacing => 0, :cellpadding => 0
    )
  end  
  
  def warp_text(text)
    content_tag(:div, text.body, :class => "article_text", :id => "items-#{text.id}")
  end
  
  def warp_code(source)
    content_tag(:div, show_code(source.body, source.language_type, "twilight"), :class => "article_source clearfix", :id => "items-#{source.id}")
  end
  
  def show_tag_list(article)
    "Tags: #{article.tags.collect{|tag| link_to tag, :controller => :welcome, :action => :list, :tag => tag.name}.join(" ")}" unless article.tags.blank?
  end
end
